import { routerRedux } from 'dva/router';
import { stringify } from 'qs';
import { fakeAccountLogin, getFakeCaptcha ,logout} from '@/services/api';
import { setAuthority } from '@/utils/authority';
import { getPageQuery } from '@/utils/utils';
import { reloadAuthorized } from '@/utils/Authorized';

export default {
  namespace: 'login',

  state: {
    status: undefined,
  },

  effects: {
    *login({ payload }, { call, put }) {
      console.log('payload',payload)
      let response = yield call(fakeAccountLogin, {...payload});
      if (response.status === 200) {
        let management_companies=[
          {
            "id": "",
            "name": "全平台",
            "address": "全平台",
            "telephone": "",
            "remark": "13999040996",
            "created_at": "2021-10-16 12:17:24"
          },
          {
            "id": "3078522871200698369",
            "name": "万泉智慧水务平台",
            "address": "葡萄镇",
            "telephone": "",
            "remark": "13999040996",
            "created_at": "2021-10-16 12:17:24"
          },
          {
            "id": "307852871200698369",
            "name": "葡萄镇供水分站",
            "address": "葡萄镇",
            "telephone": "",
            "remark": "13999040996",
            "created_at": "2021-10-16 12:17:24"
          },
          {
            "id": "312210288579473408",
            "name": "三堡乡供水分站",
            "address": "三堡乡",
            "telephone": "",
            "remark": "13394952338",
            "created_at": "2021-10-28 12:52:13"
          },
          {
            "id": "312219703432998912",
            "name": "艾丁湖供水分站",
            "address": "艾丁湖镇",
            "telephone": "0995-8610219",
            "remark": "",
            "created_at": "2021-10-28 13:29:38"
          },
          {
            "id": "312220189364088832",
            "name": "亚尔乡供水分站",
            "address": "亚尔镇",
            "telephone": "15299303459",
            "remark": "13779000829",
            "created_at": "2021-10-28 13:31:33"
          },
          {
            "id": "312220447305396224",
            "name": "恰特喀勒乡分站",
            "address": "恰特喀勒乡",
            "telephone": "",
            "remark": "18999464121",
            "created_at": "2021-10-28 13:32:35"
          },
          {
            "id": "312220736041279488",
            "name": "胜金乡供水分站",
            "address": "胜金乡",
            "telephone": "0995-8681431/15026261700",
            "remark": "15109950070",
            "created_at": "2021-10-28 13:33:44"
          },
          {
            "id": "312220955797643264",
            "name": "二堡乡供水分站",
            "address": "火焰山镇",
            "telephone": "",
            "remark": "18999468186",
            "created_at": "2021-10-28 13:34:36"
          },
          {
            "id": "3122207360412279488",
            "name": "七泉湖分公司",
            "address": "胜金乡",
            "telephone": "0995-8681431/15026261700",
            "remark": "15109950070",
            "created_at": "2021-10-28 13:33:44"
          },
          {
            "id": "3122209557297643264",
            "name": "大沿河分公司",
            "address": "火焰山镇",
            "telephone": "",
            "remark": "18999468186",
            "created_at": "2021-10-28 13:34:36"
          }
        ]
        let company_sign='all'
        if(response.data.data.management_companies.length===6){
          company_sign='e'
        }else if(response.data.data.management_companies.length===4){
          company_sign='w'
        }else if(response.data.data.management_companies.length===10){
          company_sign='all'
        }
        sessionStorage.setItem('username', response.data.data.username);
        sessionStorage.setItem('user_id', response.data.data.id);
        sessionStorage.setItem('staff_id', response.data.data.staff_id?response.data.data.staff_id:'');
        sessionStorage.setItem('token', response.data.data.token);
        sessionStorage.setItem('role_name', response.data.data.role_name);
        // sessionStorage.setItem('company_id', response.data.data.company_id?response.data.data.company_id:'3078522871200698369');
        // sessionStorage.setItem('company_name', response.data.data.company_name?response.data.data.company_name:'万泉智慧水务平台');
        sessionStorage.setItem('management_companies', response.data.data.management_companies?
          JSON.stringify(response.data.data.management_companies) :JSON.stringify(management_companies))
        sessionStorage.setItem('company_sign', company_sign)
        sessionStorage.setItem('permissions', JSON.stringify(response.data.data.permissions));
        sessionStorage.setItem('showLock','')
        yield put({
          type: 'changeLoginStatus',
          payload: response,
        });
        const urlParams = new URL(window.location.href);
        const params = getPageQuery();
        let { redirect } = params;
        console.log('redirect',redirect)
        console.log('go to /')
        // yield put(routerRedux.replace(redirect || '/'));
        window.location.href='/dashboard'
        // location.reload();
      }
    },


    *logout(_, { call, put }) {
      const response = yield call(logout);
      console.log(response);
      // let company_code=sessionStorage.getItem('company_code');
      // console.log('company_code',company_code)
      sessionStorage.clear();
      yield put({
        type: 'changeLoginStatus',
        payload: {
          status: false,
        },
      });
      yield put(
        routerRedux.push({
          pathname: '/user/login',
          // search: stringify({
          //   redirect: window.location.href,
          // }),
        })

      );

      location.reload();
    },
  },

  reducers: {
    changeLoginStatus(state, { payload }) {
      return {
        ...state,
        status: payload.status,
        type: payload.type,
      };
    },
  },
};
